package com.example.pc.recyclerview_article_xx.model;

import android.content.pm.PackageInstaller;

import com.example.pc.recyclerview_article_xx.bean.ArticleBean;
import com.example.pc.recyclerview_article_xx.iface.ArticleIface;
import com.example.pc.recyclerview_article_xx.iface.Articlelistener;
import com.example.pc.recyclerview_article_xx.service.ArticleService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by pc on 2017/4/25.
 */

public class ArtickeModel implements ArticleIface{
    Retrofit retrofit;
    private static String BASEURL="http://amicool.neusofr.edu.cn/";
    private  static String HeadPhotoNameDefault="user_headphoto.png";

    public ArtickeModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final Articlelistener listener) {
        ArticleService articleService=retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call=articleService.getResult(mod,page,sessionID );
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.body()!=null && response.isSuccessful()){
                    listener.onResponse(response.body());
                }
                else {
                    listener.onFail("onResponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}
